﻿Imports System.Data.OleDb
Public Class List
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        If (Not IsPostBack) Then
            BindRoleDropDownList()
            BindNationDropDownList()
            BindCricketersGridView()
        End If

    End Sub
    Protected Sub btnSearch_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnSearch.Click

        BindCricketersGridView()

    End Sub

    Private Sub BindRoleDropDownList()
        Dim dsRole = DataAccessLayer.ExecuteDataSet("Select DISTINCT Role from  Player")
        ddlRole.DataSource = dsRole
        ddlRole.DataTextField = "Role"
        ddlRole.DataValueField = "Role"
        ddlRole.DataBind()
        ddlRole.Items.Insert(0, New ListItem("Select", "0"))
    End Sub

    Private Sub BindNationDropDownList()
        Dim dsNation = DataAccessLayer.ExecuteDataSet("Select ID,Nation from  InternationalCricketTeams")
        ddlNation.DataSource = dsNation
        ddlNation.DataTextField = "Nation"
        ddlNation.DataValueField = "ID"
        ddlNation.DataBind()
        ddlNation.Items.Insert(0, New ListItem("Select", "0"))
    End Sub

    Private Sub BindCricketersGridView()

        Dim defaultSqlBind As String = "SELECT Player.Name, Player.DOB, Player.Nickname, Player.BattingStyle, Player.BowlingStyle, Player.Role, Player.PlayerPhoto, InternationalCricketTeams.NationFlag, InternationalCricketTeams.Nation, Player.ID, InternationalCricketTeams.TeamLogo FROM (InternationalCricketTeams INNER JOIN Player ON InternationalCricketTeams.ID = Player.Nation)"

        If ((ddlNation.SelectedItem.ToString() <> "Select") And (ddlRole.SelectedItem.ToString() <> "Select")) Then

            defaultSqlBind = defaultSqlBind & " where InternationalCricketTeams.Nation = '" & ddlNation.SelectedItem.ToString() & "' and Player.Role  = '" & ddlRole.SelectedItem.ToString() & "'"

        ElseIf (ddlNation.SelectedItem.ToString() <> "Select" And ddlRole.SelectedItem.ToString() = "Select") Then

            defaultSqlBind = defaultSqlBind & " where InternationalCricketTeams.Nation = '" & ddlNation.SelectedItem.ToString() & "'"

        ElseIf (ddlNation.SelectedItem.ToString() = "Select" And ddlRole.SelectedItem.ToString() <> "Select") Then

            defaultSqlBind = defaultSqlBind & " where Player.Role  = '" & ddlRole.SelectedItem.ToString() & "'"

        End If

        Dim ds = DataAccessLayer.ExecuteDataSet(defaultSqlBind)
        gvPlayers.DataSource = ds
        gvPlayers.DataBind()
        Dim dataView As DataView = New DataView(ds.Tables(0))
        Session("gvDS") = dataView

    End Sub

    Protected Sub btnClearSearch_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnClearSearch.Click
        BindRoleDropDownList()
        BindNationDropDownList()
        BindCricketersGridView()
    End Sub

    Protected Sub gvPlayers_Sorting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewSortEventArgs) Handles gvPlayers.Sorting
        If Not IsDBNull(Session("gvDS")) Then
            Dim dataView As DataView = Session("gvDS")

            If (GridViewSortDirection = SortDirection.Ascending) Then
                GridViewSortDirection = SortDirection.Descending
                dataView.Sort = e.SortExpression & " DESC"
            Else
                GridViewSortDirection = SortDirection.Ascending
                dataView.Sort = e.SortExpression & " ASC"
            End If

            gvPlayers.DataSource = dataView
            gvPlayers.DataBind()

            Session("gvDS") = gvPlayers.DataSource
        End If
    End Sub

    Private Property GridViewSortDirection As SortDirection
        Get
            If IsDBNull(ViewState("sortDirection")) Then
                ViewState("sortDirection") = SortDirection.Ascending
            End If
            Return ViewState("sortDirection")
        End Get
        Set(ByVal value As SortDirection)
            ViewState("sortDirection") = value
        End Set
    End Property

    Protected Sub ImageButton1_Click(ByVal sender As Object, ByVal e As System.Web.UI.ImageClickEventArgs) Handles ImageButton1.Click
      
        Response.Redirect("RssFeed.aspx")
    End Sub

    'Protected Sub btnAddCricketer_Click(sender As Object, e As EventArgs) Handles btnAddCricketer.Click
    '    Response.Redirect("AddToBlog/AddCricketer.aspx")
    'End Sub
End Class